import md5 from 'js-md5'

function getCanvas() { // 构建canvas，之后将绘制的图像转为base64编码
    let canvas = document.createElement('canvas');
    let ctx = canvas.getContext("2d");
    let txt = "TestCvs-$%^&";
    ctx.textBaseline = "top";
    ctx.font = "14px 'Arial'";
    ctx.textBaseline = "tencent";
    ctx.fillStyle = "#f60";
    ctx.fillRect(125,1,62,20);
    ctx.fillStyle = "#069";
    ctx.fillText(txt, 2, 15);
    ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
    ctx.fillText(txt, 4, 17);
 
    return canvas.toDataURL().replace("data:image/png;base64,","");
}

let browerInfo = { // 用于保存浏览器信息
    appCodeName : navigator.appCodeName,
    buildID : navigator.buildID,
    cookieEnabled : navigator.cookieEnabled,
    doNotTrack : navigator.doNotTrack,
    languages : navigator.languages.join(">"),
    oscpu : navigator.oscpu,
    platform : navigator.platform,
    userAgent : navigator.userAgent,
    height : screen.height,
    width : screen.width,
    canvas: getCanvas()
}

export function getFP(){
    let tmpKey = "";
    for(let key in browerInfo){
        tmpKey += browerInfo[key];
    }
    return md5(tmpKey);
}

export default getFP;
